package com.common.shared.usermanager;

import java.util.ArrayList;
import java.util.HashMap;

import com.common.shared.SharedClass;
import com.google.gwt.user.client.rpc.IsSerializable;

public class SUser extends SharedClass implements IsSerializable {

	private User user;
	private ArrayList<Permission> permitions;
	private ArrayList<Group> groups;

	transient HashMap<Integer, Permission> permitionIds;
	transient HashMap<String, Permission> permitionNames;

	transient HashMap<Integer, Group> groupIds;
	transient HashMap<String, Group> groupNames;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public ArrayList<Permission> getPermitions() {
		if (permitions == null) {
			permitions = new ArrayList<Permission>();
		}
		return permitions;
	}

	public void setPermitions(ArrayList<Permission> permitions) {
		this.permitions = permitions;
	}

	public ArrayList<Group> getGroups() {
		if (groups == null) {
			groups = new ArrayList<Group>();
		}
		return groups;
	}

	public void setGroups(ArrayList<Group> groups) {
		this.groups = groups;
	}

	public void removePermition(int permition_id) {
		rechekNulls();
		permitionIds.remove(permition_id);
		ArrayList<Permission> permitions = getPermitions();
		for (Permission permition : permitions) {
			if (permition.getId() == permition_id) {
				permitions.remove(permition);
				permitionNames.remove(permition.getPermission_name());
				return;
			}
		}
	}

	public void removeGroup(int group_id) {
		ArrayList<Group> groups = getGroups();
		for (Group group : groups) {
			if (group.getId() == group_id) {
				groups.remove(group);
				return;
			}
		}
	}

	public void addPermitionIfNotExists(Permission perm) {
		ArrayList<Permission> permitions = getPermitions();
		for (Permission permition : permitions) {
			if (permition.getId() == perm.getId()) {
				return;
			}
		}
		permitions.add(perm);
	}

	public void regeneratePermitionsAndGroups() {
		rechekNulls();

		permitionIds.clear();
		groupIds.clear();

		ArrayList<Permission> permitions = getPermitions();
		user.setPermitions(permitions);
		for (Permission permition : permitions) {
			permitionIds.put(permition.getId(), permition);
			permitionNames.put(permition.getPermission_name(), permition);
		}

		ArrayList<Group> groups = getGroups();
		user.setGroups(groups);
		for (Group group : groups) {
			groupIds.put(group.getId(), group);
			groupNames.put(group.getGroup_name(), group);
		}
	}

	private void rechekNulls() {
		if (permitionIds == null)
			permitionIds = new HashMap<Integer, Permission>();
		if (permitionNames == null)
			permitionNames = new HashMap<String, Permission>();
		if (groupIds == null)
			groupIds = new HashMap<Integer, Group>();
		if (groupNames == null)
			groupNames = new HashMap<String, Group>();
	}

	public boolean hasPermition(int permitionId) {
		return permitionIds.get(permitionId) != null;
	}

	public boolean hasPermition(String permitionName) {
		Object obj = permitionNames.get(permitionName);
		return obj != null;
	}

	@Override
	public long getIdVal() {
		return getUser().getId();
	}

	@Override
	public String getTextVal() {
		return getUser().getUsername();
	}

}
